var $perm = {
	export:function(){
		if(!$file){return;}
		
		var obj=this.find("*");
		var xml=""

		for (var i=0;i<obj.length;i++){
			xml+="<item type='"+obj[i].key+"' host='"+obj[i].host+"' status='"+obj[i].value+"'/>";
		}

		var objFile=$file.init($file.picker("save",{"XML ファイル":"*.xml"}).path);
		$file.write(objFile,"<permission>"+xml+"</permission>");
	},

	import:function(){
		var objFile=$file.init($file.picker("",{"XML ファイル":"*.xml"}).path);
		var str=$file.read(objFile);
		var parser = new DOMParser();
		var doc = parser.parseFromString(str, "text/xml");
		var items=doc.getElementsByTagName("item");
		
		for (var i=0;i<items.length;i++){
		//	try{
				var host=items[i].getAttribute("host");
				var key=items[i].getAttribute("type");
				var action=items[i].getAttribute("status");
				this.set("http://"+items[i].getAttribute("host"),items[i].getAttribute("type"),items[i].getAttribute("status"));
			//this.log(key)
	//		}catch(e){
		//		continue;
	//		}
		}
	},

	getDefault :function(type){
		switch (type){
			case 'cookie':
				if (this.prefs.getIntPref("network.cookie.cookieBehavior") == 2){return 2;}
				if (this.prefs.getIntPref("network.cookie.lifetimePolicy") == 2){return 8;}
				return 1;
				break;
			case "inastall":
				if (this.prefs.getBoolPref("xpinstall.whitelist.required")){return 2};
				return 1;
			default:
				return 0;
		}
	},

	mngr: Components.classes["@mozilla.org/permissionmanager;1"].getService(Components.interfaces.nsIPermissionManager),
	prefs: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2),

	get ioSvc(){
		return Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
	},
	
	getURI :function (host){
		if(typeof(host)=="object"){return host;}
		try{
			var objURI=this.ioSvc.newURI(host, null, null);
		}catch(e){
			this.log(e)
		}
		return objURI;
	},


	
	find:function(val){
		if (val.match(/^#/)){
			var type=true;
			val=val.replace(/^#/,'');
		}else if(val=="*"){
			var type=true;
		}else if(val.match(/^\?/)){
			var search=true
			val=val.replace(/^\?/,'');
		}else{
			var type=false
			host=this.type(val).host;
		}

		var obj =[];
		var enumerator = this.mngr.enumerator;
		while (enumerator.hasMoreElements()) {
			var temp = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);

			if(search){
				var regObj = new RegExp(val, "ig");
				if (temp.host.match(regObj)) {
					obj.push({"host":temp.host, "key":temp.type, "value":temp.capability});
				}
				continue;
			}
			if(type){
				if(temp.type==val || val=="*"){
					obj.push({"host":temp.host, "key":temp.type, "value":temp.capability});
				}
			}else{
				
				if(temp.host==host){
					eval("obj['"+temp.type+"']='"+temp.capability+"'");
					eval("obj['host']='"+temp.host+"'");
				}
			}
		}
		return obj;
	},

	has:function(host,type){
		var objUri=this.type(host);
		if(type){
			return this.mngr.testPermission(objUri,type);
		}else{
			var obj={
				host:objUri.host,
				image:this.mngr.testPermission(objUri,"image"),
				cookie:this.mngr.testPermission(objUri,"cookie"),
				install:this.mngr.testPermission(objUri,"install"),
				firebug:this.mngr.testPermission(objUri,"firebug"),
				download:this.mngr.testPermission(objUri,"download"),
				popup:this.mngr.testPermission(objUri,"popup"),
				script:this.mngr.testPermission(objUri,"script"),
				stylesheet:this.mngr.testPermission(objUri,"stylesheet"),
				document:this.mngr.testPermission(objUri,"document"),
				subdocument:this.mngr.testPermission(objUri,"subdocument"),
				object:this.mngr.testPermission(objUri,"object")
			}
			return obj;
		}
	},
	get:function(host,key){

//		var objReg = new RegExp(/^!/i);
//		if (host.match(objReg)){
//			var exact=true;
//			host.replace(/^!/,'');
//		}else{
//			var exact=false
//		}

		var uri=this.type(host);
		var enumerator = this.mngr.enumerator;
		while (enumerator.hasMoreElements()) {
			var temp = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
			if(temp.host==uri.host && temp.type==key){
	//			if(exact){
		//			return this.mngr.testExactPermission(uri, key);
		//		}else{
					return this.mngr.testPermission(uri,key);
			//	}
			}
		}
	},
	
	set:function(host,key,action){
		var uri=this.type(host);
		this.mngr.add(uri, key,action);
	},
	
	reset:function(host,key){
		if(host=="*"){
			this.mngr.removeAll();
		}
		var uri=this.type(host);
		this.mngr.remove(uri.host, key);
	},
	
	type:function(host){
		//this.log(typeof(host))
		switch (typeof(host)){
			case "object":
				return host;
				break;
			case "string":
			default:
//				var objReg = new RegExp(/[^http|^https|^ftp]{1}:\/\//i);
				var objReg = new RegExp(/^http:\/\//i);
				var bool=host.match(objReg);
			//	this.log(bool)
				if(!bool){var host="http://"+host;}
				return this.getURI(host);
				break;
		}
	},
	log: function(str) {
		Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService).logStringMessage(str);
	},
};